#!/bin/sh

ARCHS=`ls`
CHMOD_EXEC="chmod a+rx"
CHMOD_READ="chmod a+r"
CHMOD_WRITE="chmod +w"
CHMOD_UNWRITE="chmod -w"
CORRUPT=NO

: Check for correct usage
if [ $# -gt 1 ] ; then
  echo "Usage: ./common/bin/install_pkg [ linda_path ]" 1>&2
  exit 1
fi

: Determine the value for LINDA_PATH
if [ $# -gt 0 ] ; then
  echo "Info: distribution path specified on command line"

  LINDA_PATH=$1
  echo "Info: user specified directory: $LINDA_PATH"
  if [ ! -d $LINDA_PATH ] ; then
    echo "Error: user specified directory doesn't exist" 1>&2
    echo "Usage: ./common/bin/install_pkg [ linda_path ]" 1>&2
    exit 2
  fi

  UTILS=$LINDA_PATH/common/bin
  if [ ! -d $UTILS ] ; then
    echo "Error: \"$LINDA_PATH\" doesn't look like a Linda distribution" 1>&2
    echo "Usage: ./common/bin/install_pkg [ linda_path ]" 1>&2
    exit 3
  fi
else
  echo "Info: distribution path not specified on command line"

  LINDA_PATH=`pwd`
  echo "Info: using current working directory: $LINDA_PATH"
  if [ ! -d $LINDA_PATH ] ; then
    echo "Error: directory returned by pwd doesn't exist" 1>&2
    exit 4
  fi

  UTILS=$LINDA_PATH/common/bin
  if [ ! -d $UTILS ] ; then
    echo "Error: \"$LINDA_PATH\" doesn't look like a Linda distribution" 1>&2
    echo "Error: install_pkg must be executed from top of distribution" 1>&2
    echo "Usage: ./common/bin/install_pkg [ linda_path ]" 1>&2
    exit 5
  fi

  P=`echo $LINDA_PATH | sed 's|^/tmp_mnt/net/|/net/|'`
  if [ $P != $LINDA_PATH -a -d $P -a -d $P/common/bin ] ; then
    LINDA_PATH=$P
    echo "Info: stripped off /tmp_mnt prefix"
  fi
fi


: Determine value for FIXPATH_CMD
UTILS=$LINDA_PATH/common/bin
FIXPATH=$UTILS/fixpath
if [ ! -f $FIXPATH ] ; then
  echo "Warning: unable to find \"$FIXPATH\"" 1>&2
  echo "Warning: shell scripts will have bad LINDA_PATH values" 1>&2
  CORRUPT=YES
  FIXPATH=:
fi
FIXPATH_CMD="/bin/sh $FIXPATH"

echo "Info: executing \"chmod a+r\" on all files in ./common"
find ./common -type f -exec chmod a+r {} \;
echo "Info: executing \"chmod a+rx\" on all directory files in ./common"
find ./common -type d -exec chmod a+rx {} \;

GOOD_ARCHS=
SEP=
first=1

for i in $ARCHS
do
  if [ $i = "common" ] ; then continue ; fi
  MACH=$LINDA_PATH/$i
  if [ -d $MACH ] ; then
    LIB=$MACH/lib
    if [ -d $LIB ] ; then
      GOOD_ARCHS="${GOOD_ARCHS}${SEP}${i}"
      SEP=" "
      if [ $first = 1 ] ; then
	DEFAULT_ARCH=$i
	first=0
      fi
    fi
  fi
done

if [ ! -n "${GOOD_ARCHS}" ] ; then
  echo "Error: can't find any valid architecture directories" 1>&2
  exit 5
fi

echo " "
echo "Enter architecture to install ($GOOD_ARCHS):"
read answer
answer=`echo $answer | sed 's/ //'`
if [ ! -n "${answer}" ] ; then
  answer=$DEFAULT_ARCH
fi

MACH=$LINDA_PATH/$answer

if [ ! -d $MACH ] ; then
  echo "Error: directory \"$MACH\" does not exist" 1>&2
  exit 5
else
  echo "Info: using \"$MACH/\" as value of LINDA_PATH"

  echo "Info: executing \"chmod a+r\" on all files in" $MACH
  find $MACH -type f -exec chmod a+r {} \;
  echo "Info: executing \"chmod a+rx\" on all directory files in" $MACH
  find $MACH -type d -exec chmod a+rx {} \;

  BIN=$MACH/bin
  if [ -d $BIN ] ; then
    if [ $FIXPATH != ":" ] ; then
      echo "Info: fixing LINDA_PATH in shell scripts in directory: $BIN"
      case $answer in
      cray-t3d-ymp.8.0.2.4)
         find $BIN -type f ! -name '*.bak' -exec ${FIXPATH_CMD} ${MACH} {} \;
         ;;
      *)
         find $BIN -type f ! -name '*.bak' -exec ${FIXPATH_CMD} ${MACH} {} \; \
                                           -exec echo Info: fixed {} \;
         ;;
      esac
    fi

    echo "Info: executing \"$CHMOD_EXEC\" on all files in directory: $BIN"
    find $BIN -type f -exec $CHMOD_EXEC {} \;
  else
    echo "Warning: no bin directory in \"$MACH\"" 1>&2
    CORRUPT=YES
  fi
fi

if [ $CORRUPT = YES ] ; then
  echo "Warning: distribution may be corrupt - see previous warning(s)" 1>&2
  echo "Warning: install_pkg should be rerun after correcting the problem" 1>&2
fi

exit 0
